package com.project.seckill.rocketmq;

import com.alibaba.fastjson.JSONObject;
import com.project.seckill.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RocketMQMessageListener(topic = "seckill",consumerGroup = "seckill_stock",selectorExpression = "increate_sales")
public class IncreaseSalesConsumer implements RocketMQListener<String> {

    @Autowired
    private ProductService productService;

    @Override
    public void onMessage(String message) {
        JSONObject param = JSONObject.parseObject(message);
        Long productId = Long.valueOf(param.getString("productId"));
        int num =  param.getInteger("num");
        try {
            log.info("更新商品库存");
            productService.increaseSales(productId, num);
        }catch (Exception e) {
            log.info("更新庫存失敗:" + e.getMessage());
        }

    }
}
